package com.acme.domain.client;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import com.acme.exceptions.ClientExistsException;

public class Bank implements Serializable {

	private static final long serialVersionUID = 1L;
	Set<Client> clients = new HashSet<Client>();

	public Client addClient(final Client client) throws ClientExistsException {
		if (getClientByName(client.getName()) != null) {
			throw new ClientExistsException("Client with name: "
					+ client.getName() + " has already in the bank", client);
		}

		clients.add(client);
		return client;
	}

	public Client getClientByName(final String name) {
		for(Client client: this.clients) {
			if(client.getName() == name) {
				return client;
			}
		}
		return null;
	}

	@Override
	public String toString() {
		StringBuffer clientsString = new StringBuffer("The bank has: \n");
		for(Client client: clients) {
			clientsString.append(String.format("\t* %s\n" , client.getName()));
		}	
		return clientsString.toString();
	}

	public Set<Client> getClientsList() {
		return this.clients;
	}
}